package gui.applets;

import java.applet.Applet;
import java.awt.Button;
import java.awt.Event;
import java.awt.List;
import java.awt.TextArea;

public class AppletHandleEvent extends Applet {
	String[] flavors = { "Chocolate", "StrawBerry", "Vanilla" };

	List list = new List(6, true);

	TextArea ta = new TextArea(flavors.length, 30);
	Button btn = new Button("Test");

	int count = 0;

	public void init() {
		ta.setEditable(false);
		for (int i = 0; i < 2; i++) {
			list.addItem(flavors[count++]);
		}
		add(ta);
		add(list);
		add(btn);
	}

	public boolean handlerEvent(Event evt) {
		if (evt.id == Event.LIST_SELECT || evt.id == Event.LIST_DESELECT) {
			if (evt.target.equals(this.list)) {
				ta.setText("");
				String[] items = list.getSelectedItems();
				for (int i = 0; i < items.length; i++) {
					ta.appendText(items[i] + "\n");
				}
			} else {
				return super.handleEvent(evt);
			}
		} else
			return super.handleEvent(evt);
		return true;
	}

	public boolean action(Event evt, Object obj) {
		if (evt.target.equals(btn)) {
			if (count < flavors.length) {
				list.addItem(flavors[count++], 0);
			}
		} else
			return super.action(evt, obj);
		return true;

	}
}
